<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class OrderController extends Controller
{
    //订单列表
    public function order_list(){
    	$arr=DB::table('order')->join('order_info','order_info.oid','=','order.id')->join('goodssize','goodssize.id','=','order_info.sid')->join('goods','goods.id','=','goodssize.gid')->join('vip','vip.id','=','order.uid')->select('goods.id','name','goods.img','price','color','size','num','order_info.state','order.o_id','username','address','order_info.id as ogid')->orderBy('order.id','desc')->get();
    	return view('/admin/order/order_list',['arr'=>$arr]);
    }
    // 发货
    public function delivergoods(Request $request){
    	$gid=DB::table('order_info')->join('goodssize','goodssize.id','=','order_info.sid')->where('order_info.id',$request->ogid)->value('gid');
    	$sid=DB::table('order_info')->where('id',$request->ogid)->value('sid');
    	// 更新订单状态
    	if(DB::table('order_info')->where('id',$request->ogid)->update(['state'=>'2'])){
    		// 更新商品销售量(+1)
    		DB::table('goods')->where('id',$gid)->increment('sales');
    		// 更新库存(-1)
    		DB::table('goodssize')->where('id',$sid)->decrement('stock');
    		return redirect()->action('admin\OrderController@order_list');
    	}
    }
}
